﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ParsecParser;
using ParsecClient.Classes;

namespace ParsecClient
{
    /// <summary>
    /// Interaction logic for StatsOverview.xaml
    /// </summary>
    public partial class StatsOverview : Window
    {
        public StatsOverview()
        {
            InitializeComponent();
            CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, new ExecutedRoutedEventHandler(delegate(object sender, ExecutedRoutedEventArgs args) { this.Close(); })));
        }

        public void SetStats(CombatDetails c, double absorbShieldValue)
        {
            Topmost = true;
            if (c != null && c.CombatTimeSpan.TotalSeconds > 0)
            {
                lblLengthValue.Text = String.Format("{0:mm\\:ss\\.fff}", c.CombatTimeSpan);
                lblApmValue.Text = (c.Activates / c.CombatTimeSpan.TotalMinutes).ToString("N1");
                lblDamageOutValue.Text = c.DamageOut.ToString("N0");
                lblDamageOutValue2.Text = (c.DamageOut / c.CombatTimeSpan.TotalSeconds).ToString("N0");
                lblThreatValue.Text = c.Threat.ToString("N0");
                lblThreatValue2.Text = (c.Threat / c.CombatTimeSpan.TotalSeconds).ToString("N0");
                lblHealsOutValue.Text = c.HealOut.ToString("N0");
                if (absorbShieldValue > 0)
                {
                    lblShieldValue.Text = (c.AbsorbShieldsCast * absorbShieldValue).ToString("N0");
                    lblShieldValue2.Text = ((c.AbsorbShieldsCast * absorbShieldValue) / c.CombatTimeSpan.TotalSeconds).ToString("N0");
                }
                else
                {
                    lblShieldValue.Text = "-";
                    lblShieldValue2.Text = "-";
                }
                lblHealsOutValue2.Text = (c.HealOut / c.CombatTimeSpan.TotalSeconds).ToString("N0");
                lblHealsOutValue3.Text = (c.HealOut > 0 ? ((double)c.EffectiveHealOut / (double)c.HealOut) : 0).ToString("P1");
                lblHealsOutValue4.Text = (c.EffectiveHealOut / c.CombatTimeSpan.TotalSeconds).ToString("N0");
                lblDamageInValue.Text = c.DamageIn.ToString("N0");
                lblDamageInValue2.Text = (c.DamageIn / c.CombatTimeSpan.TotalSeconds).ToString("N0");
                lblHealsInValue.Text = c.HealIn.ToString("N0");
                lblHealsInValue2.Text = (c.HealIn / c.CombatTimeSpan.TotalSeconds).ToString("N0");
            }
            else
            {
                ClearStats();
            }
        }
        public void ClearStats()
        {
            lblApmValue.Text = "";
            lblThreatValue.Text = "";
            lblLengthValue.Text = "";
            lblDamageOutValue.Text = "";
            lblHealsOutValue.Text = "";
            lblDamageInValue.Text = "";
            lblHealsInValue.Text = "";
            lblDamageOutValue2.Text = "";
            lblHealsOutValue2.Text = "";
            lblDamageInValue2.Text = "";
            lblHealsInValue2.Text = "";
            lblHealsOutValue3.Text = "";
            lblHealsOutValue4.Text = "";
            lblThreatValue2.Text = "";
            lblShieldValue.Text = "";
            lblShieldValue2.Text = "";
        }
        public double GetBackgroundOpacity()
        {
            return BorderBackground.Opacity;
        }
        public void SetBackgroundOpacity(double opacity)
        {

            BorderBackground.Opacity = opacity;
            BorderSolid.Opacity = opacity;
            BorderBlur.Opacity = opacity;
        }

        public void DragWindow(object sender, MouseButtonEventArgs args)
        {
            try
            {
                this.WindowState = WindowState.Normal;
                DragMove();
                args.Handled = true;
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", string.Format("Error on {0} Pop Out DragWindow", this.Title), ex, null, true);
            }
        }
        private void btnIncreaseOpacity_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (BorderBackground.Opacity < 1)
                {
                    BorderBackground.Opacity += .1;
                    BorderSolid.Opacity += .1;
                    BorderBlur.Opacity += .1;
                }
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", string.Format("Error on {0} Pop Out btnIncreaseOpacity_Click", this.Title), ex, null, true);
            }
        }

        private void btnDecreaseOpacity_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (BorderBackground.Opacity > 0)
                {
                    BorderBackground.Opacity -= .1;
                    BorderSolid.Opacity -= .1;
                    BorderBlur.Opacity -= .1;
                }
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", string.Format("Error on {0} Pop Out btnDecreaseOpacity_Click", this.Title), ex, null, true);
            }
        }

        private void btnIncreaseSize_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.Width < 1000)
                {
                    this.Width += 10;
                }
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", string.Format("Error on {0} Pop Out btnIncreaseSize_Click", this.Title), ex, null, true);
            }
        }

        private void btnDecreaseSize_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.Width > 100)
                {
                    this.Width -= 10;
                }
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", string.Format("Error on {0} Pop Out btnIncreaseSize_Click", this.Title), ex, null, true);
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (Application.Current.MainWindow != null)
                {
                    ((ParsecWindow)Application.Current.MainWindow).btnStats.IsChecked = false;
                }
                this.Hide();
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", string.Format("Error on {0} Pop Out btnIncreaseSize_Click", this.Title), ex, null, true);
            }
        }
    }
}
